package montaltoTests
import montaltoFood._
import montaltoNeds._
import montaltoGrids._
import montaltoController._
import org.scalatest.Spec
import org.scalatest.matchers.ShouldMatchers

/**
 * Created by IntelliJ IDEA.
 * User: Andrew Montalto
 * Date: Apr 25, 2010
 * Time: 12:29:11 PM
 * To change this template use File | Settings | File Templates.
 */

class TestGrids extends Spec with ShouldMatchers{
  describe("Test GridPiece, "){
    val grid=new GridPiece(new Coordinate(0,0))
    describe("make a grid, "){
      it("should be have a x coordinate 0"){
         print(grid.coodinate.getCoodinate)
         grid.coodinate.x should be(0)
      }
      it("should add neds"){
         grid.settleNed(NedBuilder.random)
        grid.neds.length should be(1)
      }
      it("should remove neds"){
        grid.removeNed(0)
        grid.neds.length should be (0)
      }
      it("should grow food") {
        val s = grid.foodStack.length
        grid.growFood
        s <= grid.foodStack.length should be (true)
      }
    }
    describe("make a grid array"){
      val gridArray = new Grid(8).gridArray
      it("should have grid elements in it"){
        gridArray(1)(1).coodinate.x should be (1)
      }

     
    }
    describe("Try some comands on grids"){
      var g:Grid = new Grid(8)
      var growFood:AbstractCommand = new GrowFoodCommand(g)
      val start = g.gridArray(0)(0).foodStack.length
      growFood.execute()

      val gc:GeneticCode = new GeneticCode();
      gc.generateRandom()
      g.gridArray(0)(0).settleNed(new Ned(gc))
      val  end = g.gridArray(0)(0).foodStack.length
      it("should have more food at the end than the start"){
        start > end should be(false)
      }
      it("should allow neds to fill their bags with food") {
        val start2 = g.gridArray(0)(0).neds.first.bag.currentCalories
        val cmdAddFood:AbstractCommand = new NedsAddFood(g)
        println ("Start of add" + start2)
        cmdAddFood.execute
         cmdAddFood.execute
        g = cmdAddFood.myGrid
        val end2 =   g.gridArray(0)(0).neds.first.bag.currentCalories
        println(end2)
        println(g.gridArray(0)(0).foodStack.length)
        end2 >= start2 should be(true)
      }
      it("should allow neds to eat"){
        val start2 = g.gridArray(0)(0).neds.first.bag.currentCalories
        val nedsEat:AbstractCommand = new NedsEat(g)
        println (start2)
        nedsEat.execute
        g = nedsEat.myGrid
        val end2 = g.gridArray(0)(0).neds.first.bag.currentCalories
        println(end2)
        end2 < start2 should be(true)
      }
      
      it("should turn neds"){
        val direction = g.gridArray(0)(0).neds.first.direction
        val nedTurn:AbstractCommand = new TurnCommand(g);
        nedTurn.testing = true
        nedTurn.execute()
        val NewDirection =  g.gridArray(0)(0).neds.first.direction
        NewDirection > direction should be(true)
      }

      it("should move neds if they are facing the right direction"){
        val nedMove = new MoveCommand(g)
        nedMove.testing = true
        nedMove.execute
        g.gridArray(0)(0).neds.length should be (0)
        g.gridArray(1)(1).neds.length should be (1)
      }

      it("should mate"){
        val genetic:GeneticCode = new GeneticCode();
        genetic.generateRandom()
        val fNed:Ned = new Ned(genetic)
        fNed.gender = new Female
        g.gridArray(1)(1).neds.first.gender = new Male()
        g.gridArray(1)(1).neds.first.bag.add(new Meat)
        fNed.bag.add(new Meat)
        g.gridArray(1)(1).neds.first.bag.add(new Meat)
        fNed.bag.add(new Meat)
        g.gridArray(1)(1).neds.first.executedCommand = false
        g.gridArray(1)(1).settleNed(fNed)
        g.gridArray(1)(1).neds.length should be(2)
        println("hello")

        println(fNed.gender.toString);
        println(g.gridArray(1)(1).neds.first.gender)
        println(fNed.executedCommand);
        println(g.gridArray(1)(1).neds.first.executedCommand)
        println(fNed.hasMated);
        println(g.gridArray(1)(1).neds.first.hasMated)

        val mateCmd:MateNeds = new MateNeds(g)
        mateCmd.execute

       // g.gridArray(1)(1).neds.length should be (3)
        true
      }
      it("should remove dead neds"){
         g.gridArray(1)(1).neds.first.isAlive = false
         val rmdn:AbstractCommand = new CleanDeadNeds(g)
         val totNeds=  g.gridArray(1)(1).neds.length
         rmdn.execute
         val totNedsMin1 = g.gridArray(1)(1).neds.length
         totNedsMin1 < totNeds should be(true)

      }
    }
  }
}

